#include <iostream>
#include <algorithm>
#include <queue>
#define int long long
using namespace std;

struct building{
	int t1,t2;
};


bool cmp(building a,building b){
	return a.t2<b.t2;
}


signed main(){
	priority_queue<int> q;
	int n;
	cin>>n;
	int ans=0,sum=0;
	building b[n];
	for(int i=0;i<n;i++){
		cin>>b[i].t1>>b[i].t2;
	}
	sort(&b[0],&b[n-1],cmp);
	
	
	for(int i=0;i<n;i++){
		sum += b[i].t1;
		q.push(b[i].t1);
		if(sum <= b[i].t2){
			ans++;
		}
		else{
			sum -= q.top();
			q.pop();
		}
	}
	cout<<ans;
	return 0;
}
